package pl.edu.pk.mech.kkbus.repository;

import static pl.edu.pk.mech.kkbus.staticstrings.BusProperty.BUSSES_BRAND;
import static pl.edu.pk.mech.kkbus.staticstrings.BusProperty.BUSSES_DESC;
import static pl.edu.pk.mech.kkbus.staticstrings.BusProperty.BUSSES_NUMB_PLACE;
import static pl.edu.pk.mech.kkbus.staticstrings.BusProperty.BUSSES_PARKING;
import static pl.edu.pk.mech.kkbus.staticstrings.BusProperty.BUSSES_PRODUCTION_YEAR;
import static pl.edu.pk.mech.kkbus.staticstrings.BusProperty.BUSSES_REGISTRATION;
import static pl.edu.pk.mech.kkbus.staticstrings.BusProperty.BUSSES_STATUS;
import static pl.edu.pk.mech.kkbus.staticstrings.NameEntity.NAME_ENTITY_BUSSES;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import pl.edu.pk.mech.kkbus.datastore.common.SynchronizeCommon;
import pl.edu.pk.mech.kkbus.model.Bus;
import pl.edu.pk.mech.kkbus.model.Pojo;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Repository
public class BussesDAO extends BaseDAO{
	  
	  public List<Bus> getAllBusses() {
		  List<Bus> temp = new ArrayList<Bus>();
		  Iterable<Entity> entityList = SynchronizeCommon.listEntities(NAME_ENTITY_BUSSES, null, null);
		  for (Entity entity : entityList) {
			  temp.add(new Bus(entity));
		  }
		  return temp;
	  }
	  
	  public Bus getBus(long id) {
		  Key key = KeyFactory.createKey(NAME_ENTITY_BUSSES, id);
		  return new Bus(SynchronizeCommon.findEntity(key));
	  }

	@Override
	public void fillProperty(Pojo pojo, Entity entity) {
		  Bus bus = (Bus) pojo;
		  entity.setProperty(BUSSES_BRAND, bus.getBrand());
		  entity.setProperty(BUSSES_DESC, bus.getDescription());
		  entity.setProperty(BUSSES_NUMB_PLACE, bus.getNumSittingPlace());
		  entity.setProperty(BUSSES_PRODUCTION_YEAR, bus.getProductionYear());
		  entity.setProperty(BUSSES_REGISTRATION, bus.getRegistartion());
		  entity.setProperty(BUSSES_STATUS, bus.getStatus());
		  entity.setProperty(BUSSES_PARKING, bus.getParking());
	}
}
